package com.xhwl.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.common.pojo.cent.sdata.AccountProject;
import com.xhwl.data.dao.IAccountProjectDao;
import com.xhwl.data.service.IAccountProjectService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author:
 * @create: 2020-11-25
 **/
@Service
public class AccountProjectServiceImpl extends ServiceImpl<IAccountProjectDao, AccountProject> implements IAccountProjectService {

    @Override
    public List<Integer> list(Integer accountId, List<Integer> projectIdList) {
        QueryWrapper<AccountProject> accountProjectQueryWrapper = new QueryWrapper<>();
        accountProjectQueryWrapper
                .eq(!StringUtils.isEmpty(accountId), "account_id", accountId)
                .in(!CollectionUtils.isEmpty(projectIdList), "project_id", projectIdList);
        List<AccountProject> accountProjects = this.list(accountProjectQueryWrapper);
        List<Integer> collect = null;
        if (!StringUtils.isEmpty(accountId)) {
            collect = accountProjects.stream().map(AccountProject::getProjectId).collect(Collectors.toList());
        }
        if (!CollectionUtils.isEmpty(projectIdList)) {
            collect = accountProjects.stream().map(AccountProject::getAccountId).collect(Collectors.toList());
        }
        return collect;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(Integer accountId, List<Integer> projectIdList) {
        try {
            List<AccountProject> accountProjects = projectIdList.stream().map(projectId -> {
                AccountProject accountProject = new AccountProject();
                accountProject.setAccountId(accountId);
                accountProject.setProjectId(projectId);
                return accountProject;
            }).collect(Collectors.toList());
            this.saveBatch(accountProjects);
        } catch (Exception e) {
            throw new BusinessException("新增账号项目关联关系失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Integer> accountIdList, List<Integer> projectIdList) {
        try {
            QueryWrapper<AccountProject> accountProjectQueryWrapper = new QueryWrapper<>();
            if (!CollectionUtils.isEmpty(accountIdList)) {
                accountProjectQueryWrapper.in("account_id", accountIdList);
            }
            if (!CollectionUtils.isEmpty(projectIdList)) {
                accountProjectQueryWrapper.in("project_id", projectIdList);
            }
            this.remove(accountProjectQueryWrapper);
        } catch (Exception e) {
            throw new BusinessException("删除账号项目关联关系失败");
        }
    }

    @Override
    public void deleteBatch(List<AccountProject> accountProjects) {
        try {
            accountProjects.stream().forEach(accountProject -> {
                UpdateWrapper<AccountProject> accountProjectUpdateWrapper = new UpdateWrapper<>();
                if (null != accountProject.getAccountId()) {
                    accountProjectUpdateWrapper.eq("account_id", accountProject.getAccountId());
                }
                if (null != accountProject.getProjectId()) {
                    accountProjectUpdateWrapper.eq("project_id", accountProject.getProjectId());
                }
                this.remove(accountProjectUpdateWrapper);
            });
        } catch (Exception e) {
            throw new BusinessException("删除账号项目关联关系失败");
        }
    }


    @Override
    public List<AccountProject> findByProjectIds(Integer accountId, List<Integer> projectIdList) {

        QueryWrapper<AccountProject> accountProjectQueryWrapper = new QueryWrapper<>();
        accountProjectQueryWrapper
                .eq(!StringUtils.isEmpty(accountId), "account_id", accountId)
                .in(!CollectionUtils.isEmpty(projectIdList), "project_id", projectIdList);
        List<AccountProject> accountProjects = this.list(accountProjectQueryWrapper);
        return accountProjects;
    }

    @Override
    public List<Integer> findByAccount(Account account) {
        QueryWrapper<AccountProject> accountProjectQueryWrapper = new QueryWrapper<>();
        accountProjectQueryWrapper.eq("account_id", account.getId());
        List<AccountProject> accountProjects = this.list(accountProjectQueryWrapper);
        if(!CollectionUtils.isEmpty(accountProjects)){
            List<Integer> projectIds = accountProjects.stream().map(a -> a.getProjectId()).distinct().collect(Collectors.toList());
            return projectIds;
        }
        return null;
    }
}
